package com.zaofan.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.zaofan.bean.Account;
import com.zaofan.constant.UrlConstant;
import com.zaofan.dao.base.DaoSupportImpl;
import com.zaofan.mobile.bean.Fans;

public class AccountDao extends DaoSupportImpl<Account> {

	public Fans getUserFansInfo(Integer userId) {
		try {
			String sql = "select account.id,account.nickName as userName,concat('" + UrlConstant.HOST_URL
					+ "',account.photo) as userPhoto,account.cityName as address , "
					+ "(select count(*) from dynamic where userId = " + userId + " ) as dynamicCount ,"
					+ "(select count(*) from userfollow where userId = " + userId + ") as followCount,"
					+ "(select count(*) from userfollow where otherId = " + userId + " ) as fansCount, "
					+ "(SELECT COUNT(*) FROM account WHERE referee = (SELECT mobile FROM account WHERE id= " + userId + " )) AS inviteCount "
					+ "from account " + "where account.id = " + userId + ";";
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanHandler<>(Fans.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public com.zaofan.mobile.bean.Account findAccountInfo(int userId) {
		try {
			String sql = "select account.id,account.mobile,account.nickname,account.sex,account.birthday,account.cityId,account.cityName,account.builderId,account.builderName,account.address,account.lastLoginTime,account.regeistDate,account.rank,account.score,account.money,concat('" + UrlConstant.HOST_URL
					+ "',account.photo) as photo , "
					+ "(select count(*) from dynamic where userId = " + userId + " ) as dynamicCount ,"
					+ "(select count(*) from userfollow where userId = " + userId + ") as followCount,"
					+ "(select count(*) from userfollow where otherId = " + userId + " ) as fansCount, "
					+ "(SELECT COUNT(*) FROM account WHERE referee = (SELECT mobile FROM account WHERE id= " + userId + " )) AS inviteCount "
					+ "from account " + "where account.id = " + userId + ";";
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(getDataSource());
			return runner.query(sql, new BeanHandler<>(com.zaofan.mobile.bean.Account.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
 public Account findAccountById(int userId) {

	 String sql = "select account.id,account.mobile,account.nickname,account.sex,account.birthday,account.cityId,account.cityName,account.builderId,account.builderName,account.address,account.lastLoginTime,account.regeistDate,account.rank,account.score,account.money,concat('" + UrlConstant.HOST_URL
			 + "',account.photo) as photo , "
			 + "(select count(*) from dynamic where userId = " + userId + " ) as dynamicCount ,"
			 + "(select count(*) from userfollow where userId = " + userId + ") as followCount,"
			 + "(select count(*) from userfollow where otherId = " + userId + " ) as fansCount, "
			 + "(SELECT COUNT(*) FROM account WHERE referee = (SELECT mobile FROM account WHERE id= " + userId + " )) AS inviteCount "
			 + "from account " + "where account.id = " + userId + ";";
	 System.err.println(sql);
	 QueryRunner runner = new QueryRunner(getDataSource());

	 try {
		 return runner.query(sql, new BeanHandler<>(Account.class));
	 } catch (SQLException e) {
		 e.printStackTrace();
	 }
	 return null;
 }
}
